import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { SupplierEvaluationEntity } from '../../entity/evaluation';
import { SupplierEvaluationService } from '../../service/evaluation';
import { SupplierSupplierEntity } from '../../entity/supplier';

/**
 * 供应商评价
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: SupplierEvaluationEntity,
  service: SupplierEvaluationService,
  pageQueryOp: {
    where: async ctx => {
      const { minScore, maxScore, startDate, endDate } = ctx.request.body;
      const where = [];
      if (minScore) {
        where.push(['a.overallScore >= :minScore', { minScore }]);
      }
      if (maxScore) {
        where.push(['a.overallScore <= :maxScore', { maxScore }]);
      }
      if (startDate) {
        where.push(['a.evaluationDate >= :startDate', { startDate }]);
      }
      if (endDate) {
        where.push(['a.evaluationDate <= :endDate', { endDate }]);
      }
      return where;
    },
    select: [
      'a.*',
      'b.name as supplierName',
      'b.contactPerson',
      'b.contactPhone',
    ],
    join: [
      {
        entity: SupplierSupplierEntity,
        alias: 'b',
        condition: 'a.supplierId = b.id',
        type: 'leftJoin',
      },
    ],
  },
})
export class AdminSupplierEvaluationController extends BaseController {
  @Inject()
  supplierEvaluationService: SupplierEvaluationService;
}
